//
//  YYMVideoCollectViewController.m
//  On The Load
//
//  Created by 我爱的妞 on 15/11/16.
//  Copyright © 2015年 @@叶一闽. All rights reserved.
//

#import "YYMVideoCollectViewController.h"
#import "VideoModel.h"
#import "CoreDataManager.h"
#import "XJCTimeTableViewCell.h"
#import "XJCVideoModel.h"
#import "XJCVideoPlayViewController.h"

@interface YYMVideoCollectViewController () <UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) UITableView *tableVlew;

@property (strong, nonatomic) NSMutableArray *dataArray;

@end

@implementation YYMVideoCollectViewController

- (void)viewWillAppear:(BOOL)animated{
    
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"VideoModel"];
    self.navigationItem.title = @"视频收藏";
    //向数据管理器发送请求
    _dataArray = [[CoreDataManager shareManager].managedObjectContext executeFetchRequest:request error:nil];
    [self.tableVlew reloadData];
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;

    _tableVlew = [[UITableView alloc] initWithFrame:CURRENTSCEEN style:UITableViewStylePlain];
    
    _tableVlew.delegate = self;
    _tableVlew.dataSource = self;
    
    [self.view addSubview:_tableVlew];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIndentifier = @"cell";
    
    XJCTimeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    
    if (!cell) {
        cell = [[XJCTimeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    
    VideoModel *model = [_dataArray objectAtIndex:indexPath.row];
    
    cell.model = model;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 200 * SCALEH;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    VideoModel *model = [_dataArray objectAtIndex:indexPath.row];
    NSURL *url = [NSURL URLWithString:model.highUrl];
    
    XJCVideoPlayViewController *player = [[XJCVideoPlayViewController alloc] init];
    
    player.url = url;
    
    [self presentViewController:player animated:YES completion:nil];
}

-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    //通过系统编辑按钮控制tableview的编辑状态
    [self.tableVlew setEditing:editing animated:animated];
}
//询问那些行需要编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//设置编辑样式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
//实现编辑逻辑
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    editingStyle = UITableViewCellEditingStyleDelete;
    VideoModel *videoM = [_dataArray objectAtIndex:indexPath.row];
    
    //查询然后删除
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"VideoModel"];
    NSArray *resultArray = [[CoreDataManager shareManager].managedObjectContext executeFetchRequest:request error:nil];
    for (VideoModel *model in resultArray) {
        if ([model.title isEqualToString:videoM.title]) {
            [[CoreDataManager shareManager].managedObjectContext deleteObject:videoM];
            [_dataArray removeObject:videoM];
        }
    }
    [[CoreDataManager shareManager] saveContext];
    //刷新整个tableview
    [self.tableVlew reloadData];
}

//设置删除按钮标题
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
@end
